package com.hh.vietnameseproject.ui.recite

import android.content.Context
import android.content.Intent
import android.media.MediaPlayer
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.fragment.app.Fragment
import com.hh.vietnameseproject.R
import com.hh.vietnameseproject.ui.MainActivity
import kotlinx.android.synthetic.main.recite_viewpager_item.*
import org.w3c.dom.Text

class ReciteCardFragment : Fragment() {
    lateinit var mRootView: View
    lateinit var mCardWord: TextView
    lateinit var mAudioPlay:ImageView
    lateinit var propertyText:TextView
    lateinit var cword:TextView
    lateinit var vsentence:TextView
    lateinit var csentence:TextView
    lateinit var cardInfo: CardInfo

    lateinit var disUnderstand:Button
    lateinit var understand:Button
    lateinit var linearLayout: LinearLayout


    lateinit var player:MediaPlayer
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mRootView = inflater.inflate(R.layout.recite_viewpager_item, container, false)
        return mRootView
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        linearLayout.visibility = View.INVISIBLE
        cardInfo = arguments?.getSerializable("info") as CardInfo
        val position = arguments?.getInt("position")
        val length = arguments?.getInt("length")

        if (length== (position!! +1)){
            understand.text = "完成学习"
        }
        mCardWord.text = cardInfo.vnword
        propertyText.text = cardInfo.property+"."
        cword.text = cardInfo.cnword
        vsentence.text = cardInfo.vsentence
        csentence.text = cardInfo.csentence
        initPlayer(cardInfo.audioUrl)

        mAudioPlay.setOnClickListener {
            if(!player.isPlaying){
                player.start()
            }
        }
        disUnderstand.setOnClickListener {
            linearLayout.visibility = View.VISIBLE
        }
        understand.setOnClickListener {
            if (length== (position!! +1)){
                understand.text = "完成学习"
                Toast.makeText(context,"完成学习", Toast.LENGTH_SHORT).show()
                startActivity(Intent(context, MainActivity::class.java))
            }else{
                val a = activity as ReciteAtivity
                a.nextPage(position!! +1)
            }

        }
    }

    private fun initPlayer(url:String){
        try {
            player = MediaPlayer()
            player.setDataSource("http://124.222.92.33"+url)
            player.prepare()
        }catch (e:Exception){
            e.printStackTrace()
        }

    }
    private fun initView() {
        mCardWord = mRootView.findViewById(R.id.reciteCardWord)
        mAudioPlay = mRootView.findViewById(R.id.audioPlay)
        propertyText = mRootView.findViewById(R.id.propertyText)
        cword = mRootView.findViewById(R.id.cword)
        vsentence = mRootView.findViewById(R.id.vsentence)
        csentence = mRootView.findViewById(R.id.csentence)
        disUnderstand = mRootView.findViewById(R.id.disunderstandbtn)
        understand = mRootView.findViewById(R.id.understandBtn)
        linearLayout = mRootView.findViewById(R.id.wordIsUnderstancLayout)
    }

    companion object {
        fun newInstance(info: CardInfo,position:Int,length:Int): ReciteCardFragment {
            val fragment = ReciteCardFragment()
            val bundle = Bundle()
            bundle.putSerializable("info", info)
            bundle.putInt("position",position)
            bundle.putInt("length",length)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        player.stop()
        player.release()
    }

}